// ==UserScript==
// @name         B站直播间机器人
// @namespace    http://tampermonkey.net/
// @version      1.2.0
// @description  定时发消息、自动回复、指令[#XXX 可在后台实现(专业人员)]功能，注意自动回复对自己是不生效的~；脚本菜单参考【知乎增强】。AI对话需要实现本地服务器使用7564端口/_api/chat接口本地服务（需要后续可添加配置UI），返回格式为{data:""}，可以使用阿里云智能机器人，当然可以自己实现一个机器人（例如python 的RASA），后端项目地址：https://gitee.com/kisofdeath/bili-robot
// @author       皮燕子
// @match        https://live.bilibili.com/*
// @exclude      https://live.bilibili.com/p/html/live-web-mng/**
// @icon         https://www.bilibili.com/favicon.ico
// @grant        GM_xmlhttpRequest
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// @grant        GM_openInTab
// @grant        GM_info
// @connect      127.0.0.1
// @license      bonelf.com
// ==/UserScript==

(function () {
    // http://libs.baidu.com/jquery/2.1.4/jquery.min.js
    'use strict';
    // 注意页面有个iframe导致多次执行脚本，所以添加了exclude https://live.bilibili.com/p/html/live-web-mng/index.html?...

    // id,title,tips,defaultVal
    var menu_ALL = [
        ['menu_notify', '所有配置项需要刷新页面生效!', '你懂了就好', false],
        ['menu_intervalMsg', '定时发送消息', '定时发送消息', {60: ["新来的小伙伴关注点一点~"]}],
        ['menu_reply', '自动回复', '自动回复', {"机器人在吗": ["我随时在哦~"]}],
        ['menu_ruchang', '入场事件', '舰长入场、粉丝入场、普通用户入场', {
            enterReplyNorm: '',
            fansMedalLevel: 0,
            enterReplyFans: '',
            fansMedalContent: '',
            enterReplyJianzhang: '',
            enterReplyTidu: '',
        }],
        ['menu_at', '显示@用户后缀', '@用户后缀', true],
        ['menu_enemy', 'AI对话用户[需要服务器]', '英文逗号“,”分割', '']
    ], menu_ID = [];

    const mycss = `<style class="zhihuE_SettingStyle">
            .zhihuE_SettingRoot {
                position: absolute;
                top: 50%;
                left: 50%;
                -webkit-transform: translate(-50%, -50%);
                -moz-transform: translate(-50%, -50%);
                -ms-transform: translate(-50%, -50%);
                -o-transform: translate(-50%, -50%);
                transform: translate(-50%, -50%);
                width: auto;
                min-width: 400px;
                max-width: 600px;
                height: auto;
                min-height: 150px;
                max-height: 400px;
                color: #535353;
                background-color: #fff;
                border-radius: 3px;
            }

            .zhihuE_SettingBackdrop_1 {
                position: fixed;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
                z-index: 9999;
                display: -webkit-box;
                display: -ms-flexbox;
                display: flex;
                -webkit-box-orient: vertical;
                -webkit-box-direction: normal;
                -ms-flex-direction: column;
                flex-direction: column;
                -webkit-box-pack: center;
                -ms-flex-pack: center;
                justify-content: center;
                overflow-x: hidden;
                overflow-y: auto;
                -webkit-transition: opacity .3s ease-out;
                transition: opacity .3s ease-out;
            }

            .zhihuE_SettingBackdrop_2 {
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
                z-index: 0;
                background-color: rgba(18, 18, 18, .65);
                -webkit-transition: background-color .3s ease-out;
                transition: background-color .3s ease-out;
            }

            .zhihuE_SettingRoot .zhihuE_SettingHeader {
                padding: 10px 20px;
                color: #fff;
                font-weight: bold;
                background-color: #3994ff;
                border-radius: 3px 3px 0 0;
            }

            .zhihuE_SettingRoot .zhihuE_SettingMain, .button-group {
                padding: 10px 20px;
                border-radius: 0 0 3px 3px;
            }

            .zhihuE_SettingHeader span {
                float: right;
                margin-top: 10px;
                cursor: pointer;
            }

            .bonelf-close {
                float: right;
                margin-top: 10px;
                cursor: pointer;
            }

            .zhihuE_SettingMain input {
                margin: 10px 6px 10px 0;
                cursor: pointer;
                vertical-align: middle
            }

            .zhihuE_SettingMain label {
                margin-right: 20px;
                user-select: none;
                cursor: pointer;
                vertical-align: middle
            }

            .zhihuE_SettingMain hr {
                border: 0.5px solid #f4f4f4;
            }

            [data-theme="dark"] .zhihuE_SettingRoot {
                color: #adbac7;
                background-color: #343A44;
            }

            [data-theme="dark"] .zhihuE_SettingHeader {
                color: #d0d0d0;
                background-color: #2D333B;
            }

            [data-theme="dark"] .zhihuE_SettingMain hr {
                border: 0.5px solid #2d333b;
            }

            .bonelf-close {
                display: inline-block;
                width: 22px;
                height: 4px;
                background: white;
                transform: rotate(45deg);
            }

            .bonelf-close::after {
                content: '';
                display: block;
                width: 22px;
                height: 4px;
                background: white;
                transform: rotate(-90deg);
            }

            .bonelf-finish {
                background: white;
            }

            .bonelf-finish::after {
                background: white;
            }

            .bonelf-delete {
                background: black;
                margin-top: 23px;
            }

            .bonelf-delete::after {
                background: black;
            }

            .bonelf-key {
                width: 100px;
            }

            .bonelf-val {
                width: 200px;
            }

            input.bonelf_Setting {
                padding: .375rem .75rem;
                border-radius: .25rem;
                border: 1px solid #ced4da;
            }

            input.bonelf_Setting:focus {
                border-style: solid;
                border-color: #03a9f4;
                box-shadow: 0 0 5px #03a9f4;
            }

            input.bonelf_Setting:hover {
                cursor: text;
            }

            .button-group > button {
                float: right;
                padding: .375rem .75rem;
                border-radius: .25rem;
                border: 1px solid #ced4da;
                margin: 10px;
            }

            .button-group > button:hover {
                border-color: #03a9f4;
            }

            .button-group > button:active {
                background: #03a9f4;
            }
        </style>`

    for (let i = 0; i < menu_ALL.length; i++) {
        // 如果读取到的值为 null 就写入默认值
        if (GM_getValue(menu_ALL[i][0]) == null) {
            GM_setValue(menu_ALL[i][0], menu_ALL[i][3])
        }
    }

    // 初始化注册
    registerMenuCommand();

    /**
     * 注册脚本菜单
     * 页面如果执行多次脚本将出现BUG
     */
    function registerMenuCommand() {
        if (menu_ID.length >= menu_ALL.length) { // 如果菜单ID数组多于菜单数组，说明不是首次添加菜单（有反馈），需要卸载所有脚本菜单
            for (let i = 0; i < menu_ID.length; i++) {
                GM_unregisterMenuCommand(menu_ID[i]);
            }
        }
        for (let i = 0; i < menu_ALL.length; i++) { // 循环注册脚本菜单
            menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
            if (menu_ALL[i][0] === 'menu_intervalMsg') {
                if (menu_value(menu_ALL[i][0])) {
                    menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function () {
                        customKeyValPrompt(menu_ALL[i],
                            {type: 'number', placeholder: '定时时间/s(>10s)'},
                            {type: 'text', placeholder: '文本(<20字符)'}
                        );
                    });
                }
            } else if (menu_ALL[i][0] === 'menu_reply') {
                if (menu_value(menu_ALL[i][0])) {
                    menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function () {
                        customKeyValPrompt(menu_ALL[i],
                            {type: 'text', placeholder: '关键字(可正则)'},
                            {type: 'text', placeholder: '文本(<20字符)'}
                        );
                    });
                }
            } else if (menu_ALL[i][0] === 'menu_ruchang') {
                menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function () {
                    customFormGroupPrompt(menu_ALL[i]);
                });
            } else if (menu_ALL[i][0] === 'menu_enemy') {
                menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function () {
                    customMenuPrompt(menu_ALL[i][0], menu_ALL[i][2]);
                });
            } else {
                menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3] ? '✅' : '❌'} ${menu_ALL[i][1]}`, function () {
                    menu_switch(`${menu_ALL[i][3]}`, `${menu_ALL[i][0]}`, `${menu_ALL[i][2]}`)
                });
            }
        }
        menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {
            window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/444721-b%E7%AB%99%E7%9B%B4%E6%92%AD%E9%97%B4%E6%9C%BA%E5%99%A8%E4%BA%BA/feedback', {
                active: true,
                insert: true,
                setParent: true
            });
        });
    }

    /**
     * 输入框设置
     * @param menu
     * @param keyMethod
     * @param valueMethod
     */
    function customFormGroupPrompt(menu) {
        let menuCode = menu[0];
        let menuName = menu[1];
        let pastVal = menu_value(menuCode) || {
            enterReplyNorm: '',
            fansMedalLevel: 0,
            enterReplyFans: '',
            fansMedalContent: '',
            enterReplyJianzhang: '',
            enterReplyTidu: '',
        }
        let _html = `
        ${mycss}
        <div class="zhihuE_SettingBackdrop_1">
            <div class="zhihuE_SettingBackdrop_2"></div>
            <div class="zhihuE_SettingRoot">
                <div class="zhihuE_SettingHeader">
                    ${menuName}
                    <span class="bonelf-close bonelf-finish" title="点击关闭"></span>
                </div>
                <div class="zhihuE_SettingMain">
                    <div>
                        普通用户进场：
                            <input class="bonelf_Setting bonelf-val" name="enterReplyNorm" type="text" value="${pastVal.enterReplyNorm}" />
                    </div>
                    <div>
                        粉丝牌大于
                            <input class="bonelf_Setting bonelf-key" name="fansMedalLevel" type="number" value="${pastVal.fansMedalLevel}" />
                            级的用户进场：
                            <input class="bonelf_Setting bonelf-val" name="enterReplyFans" type="text" value="${pastVal.enterReplyFans}" />
                            <br/>
                        （我的粉丝牌：
                            <input class="bonelf_Setting bonelf-key" name="fansMedalContent" type="text" value="${pastVal.fansMedalContent}" />）
                    </div>
                    <div>
                        舰长进场：
                            <input class="bonelf_Setting bonelf-val" name="enterReplyJianzhang" type="text" value="${pastVal.enterReplyJianzhang}" />
                    </div>
                    <div>
                        提督进场：
                            <input class="bonelf_Setting bonelf-val" name="enterReplyTidu" type="text" value="${pastVal.enterReplyTidu}" />
                    </div>
                    <div>
                        tips：可用"{昵称}"表示用户昵称，为空则不会回复，配置将直接生效，发弹幕有冷却时间，请合理控制回复对象~
                    </div>
                </div>
                <div class="button-group">
                    <button class="bonelf-save">保存</button>
                </div>
            </div>
        </div>
        `
        document.body.insertAdjacentHTML('beforeend', _html);
        setTimeout(function () { // 延迟 100 毫秒，避免太快
            // 关闭按钮 点击事件
            let bonelfFinish = document.querySelector('.bonelf-finish');
            if (bonelfFinish) {
                bonelfFinish.onclick = function () {
                    this.parentElement.parentElement.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
            // 添加点击事件
            // 点击周围空白处 = 点击关闭按钮
            let bonelfDrop = document.querySelector('.zhihuE_SettingBackdrop_2');
            if (bonelfDrop) {
                bonelfDrop.onclick = function (event) {
                    this.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
            // 保存点击事件
            let bonelfSave = document.querySelector('.bonelf-save');
            if (bonelfSave) {
                bonelfSave.onclick = function (event) {
                    let newVal = {}
                    let inputs = document.querySelectorAll('input.bonelf_Setting')
                    for (let i = 0; i < inputs.length; i++) {
                        if (newVal[inputs[i].getAttribute('name')]) {
                            newVal[inputs[i].getAttribute('name')].push(inputs[i].value)
                        } else {
                            newVal[inputs[i].getAttribute('name')] = inputs[i].value
                        }
                    }
                    console.log(newVal)
                    GM_setValue(menuCode, newVal);
                    registerMenuCommand(); // 重新注册脚本菜单

                    this.parentElement.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
        }, 100)
    }

    /**
     * 输入框设置
     * @param menu
     * @param keyMethod
     * @param valueMethod
     */
    function customKeyValPrompt(menu, keyMethod, valueMethod) {
        function getItemHtml(key, value) {
            return `<div>
                        <label>
                            <input class="bonelf_Setting bonelf-key" type="${keyMethod.type}" value="${key}"
                                   placeholder="${keyMethod.placeholder}">
                        </label>
                        <label>
                            <input class="bonelf_Setting bonelf-val" type="${valueMethod.type}" value="${value}"
                                   placeholder="${valueMethod.placeholder}">
                        </label>
                        <span class="bonelf-close bonelf-delete" title="删除此行"></span>
                    </div>`
        }

        function addDelEvt() {
            let bonelfDel = document.querySelectorAll('.bonelf-delete')
            if (bonelfDel.length > 0) {
                bonelfDel.forEach(item => {
                    item.onclick = function (event) {
                        this.parentElement.remove();
                    }
                })
            }
        }

        let menuCode = menu[0];
        let menuName = menu[1];
        let pastVal = menu_value(menuCode) || {}
        let _br = '', _html = `
        ${mycss}
        <div class="zhihuE_SettingBackdrop_1">
            <div class="zhihuE_SettingBackdrop_2"></div>
            <div class="zhihuE_SettingRoot">
                <div class="zhihuE_SettingHeader">
                    ${menuName}
                    <span class="bonelf-close bonelf-finish" title="点击关闭"></span>
                </div>
                <div class="zhihuE_SettingMain">
        `
        for (let pastValKey in pastVal) {
            if (pastVal.hasOwnProperty(pastValKey)) {
                if (Array.isArray(pastVal[pastValKey])) {
                    pastVal[pastValKey].forEach(item => {
                        _html += getItemHtml(pastValKey, item)
                    })
                } else {
                    _html += getItemHtml(pastValKey, pastVal[pastValKey])
                }
            }
        }
        _html += `
                </div>
                <div class="button-group">
                    <button class="bonelf-save">保存</button>
                    <button class="bonelf-add">新增</button>
                </div>
            </div>
        </div>`
        document.body.insertAdjacentHTML('beforeend', _html); // 插入网页末尾
        setTimeout(function () { // 延迟 100 毫秒，避免太快
            // 关闭按钮 点击事件
            let bonelfFinish = document.querySelector('.bonelf-finish');
            if (bonelfFinish) {
                bonelfFinish.onclick = function () {
                    this.parentElement.parentElement.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
            // 添加点击事件
            // 点击周围空白处 = 点击关闭按钮
            let bonelfDrop = document.querySelector('.zhihuE_SettingBackdrop_2');
            if (bonelfDrop) {
                bonelfDrop.onclick = function (event) {
                    this.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
            // 点击删除按钮
            addDelEvt()
            // 添加点击事件
            let bonelfAdd = document.querySelector('.bonelf-add');
            if (bonelfAdd) {
                bonelfAdd.onclick = function (event) {
                    document.querySelector('.zhihuE_SettingMain')
                        .insertAdjacentHTML('beforeend', getItemHtml('', '')); // 插入网页末尾
                    addDelEvt()
                }
            }
            // 添加点击事件
            let bonelfSave = document.querySelector('.bonelf-save');
            if (bonelfSave) {
                bonelfSave.onclick = function (event) {
                    let keys = document.querySelectorAll('.bonelf-key')
                    let values = document.querySelectorAll('.bonelf-val')
                    let newVal = {}
                    for (let i = 0; i < keys.length; i++) {
                        if (newVal[keys[i].value]) {
                            newVal[keys[i].value].push(values[i].value)
                        } else {
                            newVal[keys[i].value] = [values[i].value]
                        }
                    }
                    if (newVal !== null) {
                        GM_setValue(menuCode, newVal);
                        registerMenuCommand(); // 重新注册脚本菜单
                    }
                    this.parentElement.parentElement.remove();
                    document.querySelector('.zhihuE_SettingStyle').remove();
                }
            }
        }, 100)
    }

    /**
     * 输入框设置
     * @param menuName
     * @param tips
     */
    function customMenuPrompt(menuName, tips) {
        let nowBlockKeywords = menu_value(menuName) || ''
        let newBlockKeywords = prompt(tips ? tips : '编辑', nowBlockKeywords);
        if (newBlockKeywords != null) {
            GM_setValue(menuName, newBlockKeywords);
            registerMenuCommand(); // 重新注册脚本菜单
        }
    }


    /**
     * 菜单开关
     * @param menuStatus
     * @param name
     * @param tips
     */
    function menu_switch(menuStatus, name, tips) {
        if (menuStatus == 'true') {
            GM_setValue(`${name}`, false);
            GM_notification({
                text: `已关闭 [${tips}] 功能\n（点击刷新网页后生效）`, timeout: 3500, onclick: function () {
                    location.reload();
                }
            });
        } else {
            GM_setValue(`${name}`, true);
            GM_notification({
                text: `已开启 [${tips}] 功能\n（点击刷新网页后生效）`, timeout: 3500, onclick: function () {
                    location.reload();
                }
            });
        }
        registerMenuCommand(); // 重新注册脚本菜单
    }


    /**
     * 返回菜单值
     */
    function menu_value(menuName) {
        for (let menu of menu_ALL) {
            if (menu[0] == menuName) {
                return menu[3]
            }
        }
    }

    window.onload = function () {
        // 消息队列
        // var msgQueue = ["您可爱的机器人来啦~(●'◡'●)"]
        var msgQueue = []

        // 消息队列冷却 s
        var msgQueueCooldown = 3;

        // readArr.forEach(item=>{msgQueue.push(item)})

        // 主播名称
        // var streamerDom = document.getElementsByClassName("room-owner-username")[0].getAttribute('title') || "Unknown"
        let streamerAreaVue = document.querySelector('.header-info-ctnr').__vue__;
        var streamerName = streamerAreaVue.anchorUsername || "Unknown"
        var liveAreaName = streamerAreaVue.liveAreaName || "Unknown"

        // 登录用户名称
        // var customerName = document.getElementsByClassName("username-info")[0].firstChild.getAttribute('title') || "Unknown"
        var customerUid;
        findVueElem('.user-panel-ctnr', (elem) => {
            customerUid = Number(elem.__vue__.userData.uid) || 0
        })

        // 正则回复JSON
        // var replyJson = {
        //     "主播你是做什么工作的": "主播就是主播啊~笨蛋~(￢‸￢) ?",
        //     "机器人？": "我是一个机器人，请多包容(●'◡'●)",
        // }
        var replyJson = {}

        // 定时消息
        var intervalMsg = []

        /**
         * 刷新配置
         */
        function initConfig() {
            // 定时消息
            var intervalMsgData = menu_value("menu_intervalMsg")
            for (let intervalMsgDataKey in intervalMsgData) {
                if (intervalMsgData.hasOwnProperty(intervalMsgDataKey)) {
                    intervalMsg.push({
                        interval: Math.max(Number(intervalMsgDataKey), 10), msgArr: intervalMsgData[intervalMsgDataKey]
                    })
                }
            }
            // 正则回复
            var replyJsonData = menu_value("menu_reply")
            for (let replyJsonDataKey in replyJsonData) {
                if (replyJsonData.hasOwnProperty(replyJsonDataKey)) {
                    replyJson[replyJsonDataKey] = replyJsonData[replyJsonDataKey][0]
                }
            }
        }

        initConfig()
        console.log("当前生效自动回复", replyJson)
        console.log("当前生效定时消息", intervalMsg)
        // var intervalMsg = [
        //     {interval: 60, msgArr: ["新来的小伙伴关注点一点~"]}
        // ]

        /**
         * AI 对话
         * @param param
         */
        function aiReply(param) {
            // AI 对话
            if (param.onlineCount && param.onlineCount > 100) {
                console.log("人数过多，AI对话禁用")
            }
            GM_xmlhttpRequest({
                method: "post",
                url: 'http://127.0.0.1:7564/_api/chat',
                data: JSON.stringify({content: param.text}),
                headers: {
                    "Content-Type": "application/json"
                },
                onload: function (res) {
                    let data
                    try {
                        data = JSON.parse(res.response);
                    } catch (e) {
                    }
                    if (data) {
                        let ctt = data.data + (param.uname && menu_value("menu_at") ? ("@" + param.uname) : "");
                        console.log("回复内容:" + ctt)
                        msgQueue.push(ctt)
                    }
                },
                onerror: function (err) {
                    console.error(err)
                }
            });
        }

        // 指令列表
        var orderJson = {
            "#测试": function (param = '') {
                // sendStr("测试正常：" + param)
                msgQueue.push("测试正常：" + param.text)
            },
            "#对话 ": function (param) {
                aiReply(param);
            }
            // 其他指令过后台
            // "#刷新配置": function (param = '') {
            //     initConfig()
            //     // sendStr("刷新成功")
            //     msgQueue.push("刷新成功")
            // }
        }
        console.log("当前生效指令", orderJson)

        // 弹幕输入框model所在元素
        var danmakuElem;

        /**
         * 获取元素
         * @param selector
         * @param callback
         */
        function findVueElem(selector, callback) {
            // 弹幕输入框model所在元素获取任务定时器
            var times = 0;
            var findElemTimer = setInterval(() => {
                var elem = document.querySelector(selector);
                if (times > 20 || (elem && elem.__vue__)) {
                    callback(elem);
                    clearInterval(findElemTimer)
                } else {
                    console.warn("获取弹幕元素失败")
                }
                times++
            }, 1000)
        }

        findVueElem("#control-panel-ctnr-box", (res) => {
            danmakuElem = res;
        });

        /**
         * 发送数组消息
         * @param arr
         * @param finish 消息发送完即关闭定时器
         */
        function sendArr(arr, finish = true) {
            var times = 0;
            var timer = setInterval(() => {
                var elem = danmakuElem;
                if (elem && elem.__vue__) {
                    let data = elem.__vue__.$data;
                    if (data && arr.length > 0) {
                        data.chatInput = arr.shift().substring(0, 20)
                        elem.__vue__.sendDanmaku()
                    }
                    if (arr.length === 0 && finish) {
                        clearInterval(timer)
                    }
                } else if (times < 20) {
                    console.warn("danmakuElem 为空")
                } else {
                    console.warn("消息队列因获取不到弹幕输入框model终止")
                    clearInterval(timer)
                }
                times++
            }, msgQueueCooldown * 1000)
        }

        /**
         * 对此方法调用可能会引起弹幕输入过快,
         * 更推荐向消息队列+数据
         * @param str
         */
        function sendStr(str) {
            var elem = danmakuElem;
            if (elem && elem.__vue__) {
                let data = elem.__vue__.$data;
                if (!data) {
                    alert("发送失败")
                }
                data.chatInput = str.substring(0, 20)
                elem.__vue__.sendDanmaku()
            }
        }

        // 消息队列数据发送
        sendArr(msgQueue, false)

        // 定时发送消息
        intervalMsg.forEach(item => {
            item.timer = setInterval(() => {
                item.msgArr.forEach(i => {
                    msgQueue.push(i)
                })
            }, item.interval * 1000)
        })

        // 处理到的最近的弹幕
        var danmakuPointer

        /**
         * 初始化最近一条弹幕的DOM
         */
        function initRecentDom() {
            let allDanmakuElem = document.querySelectorAll("div.danmaku-item[data-uname]")
            if (allDanmakuElem.length > 0) {
                danmakuPointer = allDanmakuElem[allDanmakuElem.length - 1]
            }
        }

        // 消息回复
        var timer = setInterval(() => {
            if (!danmakuPointer) {
                initRecentDom();
            }
            // 以后出现弹幕过多时页面卡死则考虑加这个代码，但是要考虑高能预警情况 可能卫视 ###.##万 这种带单位的格式
            let onlineCount = document.querySelector('.header-info-ctnr').__vue__.onlineCount || 0;
            // if (onlineCount > 10000) {
            //     console.warn("本直播间人数超过10000，出于页面安全考虑禁用了自动回复功能")
            // }
            while (danmakuPointer && danmakuPointer.nextSibling != null) {
                danmakuPointer = danmakuPointer.nextSibling
                if (!danmakuPointer.classList.contains('danmaku-item')) {
                    continue;
                }
                let uid = Number(danmakuPointer.getAttribute('data-uid'))
                // 跳过对自己的回复 uname === streamerName || 或者主播添加这个，其实能获取到用户id来判断
                let text = danmakuPointer.getAttribute('data-danmaku')
                // console.debug("uid:" + uid);
                // console.debug("customerUid:" + customerUid);
                if (uid !== customerUid) {
                    // 自动回复
                    for (let each in replyJson) {
                        let re = new RegExp(each);
                        if (text.match(re)) {
                            console.log("自动回复->" + each + "：" + replyJson[each])
                            // sendStr(replyJson[each] + menu_value("menu_at") ? "@" + uname : "")
                            msgQueue.push(replyJson[each] + (menu_value("menu_at") ? ("@" + uid) : ""))
                            break;
                        }
                    }
                }
                // 指令应答
                if (text.startsWith("#")) {
                    let match = false;
                    for (let each in orderJson) {
                        let re = new RegExp(each);
                        let result = text.match(re);
                        if (result && result.length > 0) {
                            match = true;
                            console.log("指令应答:" + each)
                            let param = {
                                text: text.replace(result[0], ''),
                                onlineCount: onlineCount,
                                uname: uid
                            }
                            orderJson[each](param)
                            break;
                        }
                    }
                    if (!match) {
                        // 请求后台数据
                        GM_xmlhttpRequest({
                            method: "post",
                            url: 'http://127.0.0.1:7564/_api/order',
                            data: JSON.stringify({
                                content: text,
                                streamerName: streamerName,
                                customerName: customerUid,
                                channel: liveAreaName,
                                uname: uid,
                            }),
                            headers: {
                                "Content-Type": "application/json"
                            },
                            onload: function (res) {
                                let data
                                try {
                                    data = JSON.parse(res.response);
                                } catch (e) {
                                }
                                if (data && data.data) {
                                    let ctt = data.data.content + (uid && menu_value("menu_at") ? ("@" + uid) : "");
                                    console.log("回复内容:" + ctt)
                                    msgQueue.push(ctt)
                                }
                            },
                            onerror: function (err) {
                                console.error(err)
                            }
                        });
                    }
                }
                // 对话用户
                let enemyStr = menu_value("menu_enemy");
                if (enemyStr) {
                    for (let name of enemyStr.split(",")) {
                        if (uid === name && uid !== customerUid) {
                            aiReply({text: text})
                        }
                    }
                }
            }
        }, 5000)

        /**
         * 普通用户进入直播间
         */
        document.querySelector('#brush-prompt').addEventListener('DOMNodeInserted', function (e) {
            // fans-medal-content 粉丝牌
            let $fansMedalContent = document.querySelector('#brush-prompt .fans-medal-content')
            let fansMedalContent = $fansMedalContent ? $fansMedalContent.innerHTML : null
            // fans-medal-level 粉丝等级
            let $fansMedalLevel = document.querySelector('#brush-prompt .fans-medal-level')
            let fansMedalLevel = $fansMedalLevel ? Number($fansMedalLevel.innerHTML) : 0
            // interact-name 用户昵称
            let $interactName = document.querySelector('#brush-prompt .interact-name')
            let interactName = $interactName ? $interactName.innerHTML : null
            // console.log('普通用户进入直播间', interactName);

            let ruchangSetting = menu_value("menu_ruchang") || {};
            let myFansMedalContent = ruchangSetting.fansMedalContent || null;
            let myFansMedalLevelStr = ruchangSetting.fansMedalLevel;
            // 普通用户
            let normUserReply = ruchangSetting.enterReplyNorm;
            // 粉丝
            let fansUserReply = ruchangSetting.enterReplyFans;
            // 达到要求的粉丝级别
            let myFansMedalLevel = myFansMedalLevelStr ? Number(myFansMedalLevelStr) : 0;

            if (fansUserReply && myFansMedalContent === fansMedalContent && myFansMedalLevel <= fansMedalLevel) {
                // console.log(fansUserReply
                //     .replace("{昵称}", interactName))
                msgQueue.push(fansUserReply
                    .replace("{昵称}", interactName))
            }
            // console.log("欢迎用户", normUserReply, interactName)
            if (normUserReply) {
                // console.log(normUserReply.replace("{昵称}", interactName))
                msgQueue.push(normUserReply.replace("{昵称}", interactName))
            }
        });

        /**
         * 下面事件会触发两次，所以以此字段判断第二次不执行
         */
        var lastWelcomeText;
        /**
         * 特殊用户进入直播间
         */
        document.querySelector('#welcome-area-bottom-vm').addEventListener('DOMNodeInserted', function (e) {
            let $samaNameBox = document.querySelector('#welcome-area-bottom-vm .sama-name-box')
            let welcomeText = $samaNameBox ? $samaNameBox.textContent : ""
            // console.log("特殊用户进入直播间事件触发", welcomeText)
            if (lastWelcomeText === welcomeText) {
                return;
            }

            function send(type, reply) {
                let $interactName = document.querySelector('#welcome-area-bottom-vm .sama-name-box span')
                let interactName = $interactName ? $interactName.innerHTML : null
                // console.log("欢迎" + type, jianzhangReply, interactName)
                if (reply) {
                    console.log(reply
                        .replace("{昵称}", interactName))
                    // msgQueue.push(reply
                    //     .replace("{昵称}", interactName))
                }
            }

            let ruchangSetting = menu_value("menu_ruchang") || {};
            if (welcomeText.startsWith("欢迎舰长 ")) {
                send('舰长', ruchangSetting.enterReplyJianzhang);
            } else if (welcomeText.startsWith("欢迎提督 ")) {
                send('提督', ruchangSetting.enterReplyTidu);
            }
            // 还有一个没身份的特效，应该是高能用户？
            lastWelcomeText = welcomeText;
        });

        /**
         * 清除已定义的定时器
         */
        function stopAllInterval() {
            // 定时消息
            intervalMsg.forEach(item => {
                if (item.timer) {
                    clearInterval(item.timer)
                }
                delete item.timer;
            })
            clearInterval(timer)
        }

        function keyDown(e) {
            if (e.which === 27) { //ESC
                e.returnValue = false;
                console.log("ESC")
                stopAllInterval()
                return false;
            }
        }

        document.onkeydown = keyDown;
    }
})();
